<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2018/8/17
 * Time: 14:03
 */

namespace PddWechat\Models\Pddorder\Service;

use PddWechat\Models\Pddorder\Data\PddOrderData;
use Dai\Framework\Base\BaseSessionInfo;
use PddWechat\Models\Pddorder\Param\AllSpreadListParam;
use PddWechat\Models\User\Data\UserData;

class AllSpreadList {

    public function __construct()
    {
        $this->pddData = new PddOrderData();
        $this->userData = new UserData();
    }

    public function execute($basePageInfo)
    {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        $uid = $sessionData->uid;
        /** @var AllSpreadListParam  $params*/
        $params = $basePageInfo->params;
        $pddList = $this->pddData->selectAllSpreadList($uid, $params);
        if(!empty($pddList)) {
            $uids = [];
            foreach ($pddList as $item) {
                array_push($uids, $item->uid);
            }
            $uids = array_unique($uids);
            //获取用户信息
            $doUsers = $this->userData->getUsersBuUids($uids);
            foreach ($pddList as $item) {
                foreach ($doUsers as $it) {
                    if($item->uid == $it->id) {
                        $item->userName = $it->userName;
                        $item->avatar = $it->avatar;
                    }
                }
                //处理展示的佣金
                if($item->uid == $uid) {
                    $item->userMoney = floatval($item->uidMoney/100);
                }elseif ($item->inviteUid == $uid) {
                    $item->userMoney = floatval($item->inviteUidMoney/100);
                }elseif ($item->inviteParentUid == $uid){
                    $item->userMoney = floatval($item->inviteParentUidMoney/100);
                }
            }
        }
        return $pddList;
    }
}